package cn.iocoder.yudao.module.of.service.taskgenuser;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.of.controller.admin.taskgenuser.vo.*;
import cn.iocoder.yudao.module.of.dal.dataobject.taskgenuser.TaskGenUserDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.of.dal.mysql.taskgenuser.TaskGenUserMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.of.enums.ErrorCodeConstants.*;

/**
 * 会议、收文  主责部门指派任务生成人员 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class TaskGenUserServiceImpl implements TaskGenUserService {

    @Resource
    private TaskGenUserMapper taskGenUserMapper;

    @Override
    public Long createTaskGenUser(TaskGenUserSaveReqVO createReqVO) {
        // 插入
        TaskGenUserDO taskGenUser = BeanUtils.toBean(createReqVO, TaskGenUserDO.class);
        taskGenUserMapper.insert(taskGenUser);
        // 返回
        return taskGenUser.getId();
    }

    @Override
    public void updateTaskGenUser(TaskGenUserSaveReqVO updateReqVO) {
        // 校验存在
        validateTaskGenUserExists(updateReqVO.getId());
        // 更新
        TaskGenUserDO updateObj = BeanUtils.toBean(updateReqVO, TaskGenUserDO.class);
        taskGenUserMapper.updateById(updateObj);
    }

    @Override
    public void deleteTaskGenUser(Long id) {
        // 校验存在
        validateTaskGenUserExists(id);
        // 删除
        taskGenUserMapper.deleteById(id);
    }

    private void validateTaskGenUserExists(Long id) {
        if (taskGenUserMapper.selectById(id) == null) {
            throw exception(TASK_GEN_USER_NOT_EXISTS);
        }
    }

    @Override
    public TaskGenUserDO getTaskGenUser(Long id) {
        return taskGenUserMapper.selectById(id);
    }

    @Override
    public PageResult<TaskGenUserDO> getTaskGenUserPage(TaskGenUserPageReqVO pageReqVO) {
        return taskGenUserMapper.selectPage(pageReqVO);
    }

}